package com.smx.service.impl;

import com.smx.common.service.RedisService;
import com.smx.domain.Student;
import com.smx.service.ChartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * @ClassName ChartServiceImpl
 * @Description TODO
 * @Author moses
 * @Date 2021/8/11
 **/
@Service
public class ChartServiceImpl implements ChartService {

    @Autowired
    private RedisService redisService;

    private static final String KEY_NAME = "student";

    /**
     * 添加实体排行榜
     *
     * @param s
     * @return
     */
    @Override
    public int insertChart(Student s) {
        return redisService.zAdd(KEY_NAME, s, s.getScore()) ? 1 : 0;
    }

    /**
     * 从排行榜中删除
     *
     * @param s
     * @return
     */
    @Override
    public Long removeChart(Student s) {
        return redisService.zRemove(KEY_NAME, s);
    }

    /**
     * 查询指定范围排行榜信息（由高到低）
     *
     * @return
     */
    @Override
    public Set<Object> listChart() {
        return redisService.zRange(KEY_NAME, 0, 2);
    }
}
